package com.wsd.jing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.entity.UserPost;
import com.wsd.jing.mapper.UserPostMapper;
import com.wsd.jing.service.IUserPostService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: UserPostServiceImpl
 * @description: 用户职位关联服务实现类
 * @createDate 2022-11-20 07:41
 * @copyright 2022-11-20 07:41 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserPostServiceImpl extends ServiceImpl<UserPostMapper, UserPost> implements IUserPostService {

    private final UserPostMapper userPostMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertUserPost(UserPost userPost) {
        return this.save(userPost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateUserPost(UserPost userPost) {
        return this.updateById(userPost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteUserPost(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteUserPosts(String ids) {
        List<String> idList = StrUtil.splitTrim(ids, ",");
        return this.removeByIds(idList);
    }

    @Override
    public UserPost getUserPost(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<UserPost> listUserPosts(CommonRequest commonRequest, UserPost userPost) {
        QueryWrapper<UserPost> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageUserPosts(CommonRequest commonRequest, UserPost userPost) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<UserPost> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<UserPost> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<UserPost> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
 }
